package org.eclipse.equinox.spring.osgi;
/**
 * This file is part of org.springframework.equinox.deployer. 
 * Copyright (C) olivier.evalet@genebio.com and genebio team
 * 
 * org.springframework.equinox.deployer is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * org.springframework.equinox.deployer is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * org.springframework.equinox.deployer. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Special thanks for Bernd Kolb the first author of this file  
 */

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.Servlet;

import org.eclipse.equinox.spring.osgi.deployer.OsgiSpringWebappDeployer;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.osgi.context.BundleContextAware;

/**
 * A utility bean which could be used to register http services. To use it write
 * something like the following code <br>
 * <code>
 * &lt;bean id=&quot;serviceRegistrator&quot; class=&quot;de.kolbware.springOsgiUtil.http.ServiceRegistrator&quot;&gt;<br>
 * &nbsp;&nbsp;&nbsp;&lt;constructor-arg value=&quot;de.kolbware.jpetstore.internal.web.HttpServiceTracker&quot;/&gt;<br>
 * &lt;/bean&gt;
 * </code>
 * 
 * @author Bernd Kolb
 * 
 */
public class ServiceRegistrator implements ApplicationContextAware,
		BundleContextAware, BundleListener {

	private ApplicationContext applicationContext;
	private BundleContext context;
	private Class<? extends AppContextAwareHttpServiceTracker> httpServiceTrackerClass;
	private AppContextAwareHttpServiceTracker httpServiceTracker;
	private Map<String, String> contextConfig;
	private List<SpringServlet> servlets;
	
	public void setServlets(List<SpringServlet> servlets){
		this.servlets=servlets;
	}

	public void setContextConfig(Map<String,String> config){
		this.contextConfig=config;
	}
	
	/**
	 * @param httpServiceTrackerClass
	 *            a class extending {@link AppContextAwareHttpServiceTracker}
	 */
	public ServiceRegistrator(
			Class<? extends AppContextAwareHttpServiceTracker> httpServiceTrackerClass) {
		this.httpServiceTrackerClass = httpServiceTrackerClass;
	}

	public ServiceRegistrator(String httpServiceTrackerClass) throws ClassNotFoundException {
		this.httpServiceTrackerClass = (Class<? extends AppContextAwareHttpServiceTracker>) Class.forName(httpServiceTrackerClass);
	}
	
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
		if (httpServiceTracker != null) {
			httpServiceTracker.setAppContext(applicationContext);
		}

	}

	@Override
	public void setBundleContext(BundleContext context) {
		try {
			httpServiceTracker = httpServiceTrackerClass.getConstructor(
					BundleContext.class).newInstance(context);
			httpServiceTracker.open();
			context.addBundleListener(this);
			if (applicationContext != null) {
				httpServiceTracker.setContextConfig(contextConfig);
				httpServiceTracker.setServlets(servlets);
				httpServiceTracker.setAppContext(applicationContext);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void bundleChanged(BundleEvent event) {
		
		if (context!=null && event.getBundle() == context.getBundle()
				&& event.getType() == Bundle.STOPPING) {
			httpServiceTracker.close();
		}
	}

}
